package com.yj.generator.core;

import com.yj.generator.constants.ConfigConstant;
import com.yj.generator.constants.JavaTypeConstant;
import com.yj.generator.constants.SqlConstant;
import com.yj.generator.entity.DimField;
import com.yj.generator.entity.Field;
import com.yj.generator.entity.Table;
import com.yj.generator.enums.FieldTypeEnum;
import com.yj.generator.utils.CommonUtil;
import com.yj.generator.utils.PropertiesUtil;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


import java.sql.*;
import java.util.*;


/**
 * @author DengLin
 * @title sql执行器
 * @date 2024/07/04
 */
@Data
@Builder
public class SqlExecutor {
    private static final Logger log = LogManager.getLogger(SqlExecutor.class);

    private String url;
    private String username;
    private String password;
    private String driver;

    public List<Table> getTables(List<String> tableNames) {
        Connection connection = null;
        ArrayList<Table> tables = new ArrayList<>(tableNames.size());
        try {
            Class.forName(this.driver);
            connection = DriverManager.getConnection(this.url, this.username, this.password);
            for (String tableName : tableNames) {
                Table table = new Table();
                // 构建表
                table.setTableName(tableName);
                String beanName = tableName;
                if (Boolean.valueOf(PropertiesUtil.getProperty(ConfigConstant.GENERATE_TABLE_IGNORE_PREFIX))) {
                    String[] prefixes = PropertiesUtil.getProperty(ConfigConstant.GENERATE_TABLE_PREFIX).split(",");
                    beanName = CommonUtil.removePrefix(beanName, Arrays.asList(prefixes));
                }
                beanName = CommonUtil.toCamel(beanName, true);
                table.setBeanName(beanName);
                table.setComment(beanName);
                String sql = String.format(SqlConstant.SHOW_TABLE_INFO, tableName);
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                ResultSet rs = preparedStatement.executeQuery();
                ArrayList<Field> fields = new ArrayList<>();
                ArrayList<DimField> dimFields = new ArrayList<>();
                while (rs.next()) {
                    Field field = new Field();
                    String fieldName = rs.getString("field");
                    String type = rs.getString("type");
                    String key = rs.getString("key");
                    String comment = rs.getString("comment");
                    String propertyName = CommonUtil.toCamel(fieldName, false);

                    if (type.contains("(")) {
                        type = type.substring(0, type.indexOf("("));
                    }
                    FieldTypeEnum typeEnum = FieldTypeEnum.getEnumByValue(type);
                    String javaType = typeEnum.getJavaType();
                    String jdbcType = typeEnum.getJdbcType();

                    if (JavaTypeConstant.BIG_DECIMAL.equals(javaType)) {
                        table.setHasBigDecimal(true);
                    }

                    if (JavaTypeConstant.DATE.equals(javaType)) {
                        table.setHasDateTime(true);
                    }

                    if (JavaTypeConstant.TIME.equals(javaType)) {
                        table.setHasTime(true);
                    }

                    field.setFieldName(fieldName);
                    field.setPropertyName(propertyName);
                    field.setSqlType(type);
                    field.setJavaType(javaType);
                    field.setJdbcType(jdbcType);
                    field.setComment(comment);

                    if (StringUtils.isNotEmpty(key) && SqlConstant.PRIMARY_KEY.equals(key)) {
                        field.setPrimaryKey(true);
                    }
                    fields.add(field);
                    DimField dimField = new DimField();
                    if (!ConfigConstant.IGNORE_DIM_FIELDS.contains(fieldName)){
                        dimField.setPropertyName(propertyName);
                        dimField.setJavaType(javaType);
                    }
                }
                table.setFieldList(fields);
                table.setDimFieldList(dimFields);
                tables.add(table);
            }
        } catch (Exception e) {
            log.error("数据库链接异常:{}", e.getMessage());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    log.error("数据库关闭异常:{}", e.getMessage());
                }
            }
        }

        return tables;
    }

    public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<>();
        list.add("user");




    }
}
